//
//  LHStatusOriginalView.swift
//  sina_weibo
//
//  Created by 刘刘海强 on 16/5/13.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit



class LHStatusOriginalView: UIView {
    
    var originalViewBottom: Constraint?
    
    var statusViewModel: LHStatusViewModel? {
        didSet {
            //给控件赋值
            iconImaageView .sd_setImageWithURL(NSURL(string: (statusViewModel?.status?.user?.profile_image_url)!), placeholderImage: UIImage(named: "avatar_default_big"))
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            messageLabel.text = statusViewModel?.status?.text
            messageLabel.numberOfLines = 0
    
            timeLabel.text = statusViewModel?.timeContent
            mbrankImageView.image = UIImage(named: (statusViewModel?.mbrank)!)
            sourceLabel.text = statusViewModel!.contentSource
            verifiedTypeImageView.image = statusViewModel?.verifiedType
            
            //卸载约束
            originalViewBottom?.uninstall()
            //设置配图
            if let picUrl = statusViewModel?.status?.pic_urls where picUrl.count > 0 {
                //显示配图，更新约束
                statusPictureView.picUrl = picUrl
                statusPictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBottom = make.bottom.equalTo(statusPictureView.snp_bottom).offset(StatusTableViewCellMargin).constraint
                })
            } else {
                //隐藏配图，更新约束
                statusPictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBottom = make.bottom.equalTo(messageLabel.snp_bottom).offset(StatusTableViewCellMargin).constraint
                })
            }
            
        }
    }

    //懒加载控件
    //头像
    private lazy var iconImaageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证类型
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    //昵称
    private lazy var screenNameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
    //发微博时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 12)
    //微博来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 12)
    //内容
    private lazy var messageLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
    
    private lazy var statusPictureView: LHStatusPictureView = LHStatusPictureView()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(iconImaageView)
        addSubview(verifiedTypeImageView)
        addSubview(mbrankImageView)
        addSubview(screenNameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(messageLabel)
        addSubview(statusPictureView)
        
        //设置约束
        iconImaageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImaageView.snp_trailing)
            make.centerY.equalTo(iconImaageView.snp_bottom)
        }
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(StatusTableViewCellMargin)
        }
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImaageView)
            make.leading.equalTo(iconImaageView.snp_trailing).offset(StatusTableViewCellMargin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImaageView)
            make.leading.equalTo(iconImaageView.snp_trailing).offset(StatusTableViewCellMargin)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(StatusTableViewCellMargin)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(verifiedTypeImageView.snp_bottom).offset(10)
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
        }
        statusPictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.equalTo(messageLabel)
        }
        self.snp_makeConstraints { (make) -> Void in
            self.originalViewBottom = make.bottom.equalTo(messageLabel.snp_bottom).offset(StatusTableViewCellMargin).constraint
        }
        
   
    }

}
